import { PropsWithChildren } from "react";
import Taro, { useLaunch } from "@tarojs/taro";
import { SilenceAuthLogin } from "./api/login";

import "./tail.css";
import "./app.less";
import useAppStore from "./store";

function App({ children }: PropsWithChildren<any>) {
  useLaunch((options) => {
    const updateManager = Taro.getUpdateManager();
    updateManager.onUpdateReady(function () {
      Taro.showModal({
        title: "更新提示",
        content: "新版本已准备好，须重启应用",
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate();
          }
        },
      });
    });

    const { setAppLoggingIn } = useAppStore.getState();
    setAppLoggingIn(true);

    SilenceAuthLogin()
      .then(() => {
        if (options.path === "pages/login/index") {
          Taro.reLaunch({ url: "/pages/index/index" });
        }
      })
      .finally(() => {
        setAppLoggingIn(false);
      });
  });

  // children 是将要会渲染的页面
  return children;
}

export default App;
